package org.csu.gp_store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.csu.gp_store.bo.ItemBO;
import org.csu.gp_store.entity.*;
import org.csu.gp_store.mapper.*;
import org.csu.gp_store.service.*;
import org.csu.gp_store.util.MD5Util;
import org.csu.gp_store.util.TokenUtil;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CompanyServiceImpl implements CompanyService {

    @Autowired
    CompanyMapper companyMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    OrderService orderService;

    @Autowired
    ProductService productService;

    @Autowired
    ItemService itemService;

    @Autowired
    FactoryMapper factoryMapper;

    @Autowired
    FacBusinessMapper facBusinessMapper;

    @Autowired
    ProductMapper productMapper;

    @Override
    public int dealOrder(Long indexID, int status) {
        if(orderService.getOrderByOrderID(indexID)!=null) {
            UpdateWrapper<OrderItem> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("indexID", indexID);
            updateWrapper.set("status", status);
            return orderItemMapper.update(null, updateWrapper);
        }
        return 0;
    }

    @Override
    public int adminCancel(Long orderID, String MSG) {
        if(orderService.getOrderByOrderID(orderID)!=null) {
            UpdateWrapper<OrderItem> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("orderID", orderID);
            updateWrapper.set("status", 7);
            return orderItemMapper.update(null, updateWrapper);
        }
        //TODO 这个MSG大概应该给管理员看
        return 0;
    }

    @Override
    public String companyLogin(@NotNull String email, @NotNull String password) {
        Company company = getCompanyByEmail(email);
        if(company!=null){
            if(company.getPassword().equals(MD5Util.getMD5String(password))){
                return TokenUtil.createToken(company.getCompanyID().toString());
            }
        }
        return null;
    }

    @Override
    public int insertNewCompany(Company company) {
        if(company!=null){
            return companyMapper.insert(company);
        }
        return 0;
    }

    @Override
    public int alterCompanyInfo(Company company) {
        if(company!=null){
            LambdaQueryWrapper<Company> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(Company::getEmail,company.getEmail());
            return companyMapper.update(company,lambdaQueryWrapper);
        }
        return 0;
    }

    @Override
    public int addBusiness(ItemBO itemBO) {
        //TODO 添加商品，首先判断product中是否有相应的，没有就先添加product，再添加item 好像不是很需要这个方法，先放着看看
        return 0;
    }

    @Override
    public int cancelCompany(Company company, int status) {
        if (company!=null){
            UpdateWrapper<Factory> uw = new UpdateWrapper<>();
            uw.lambda().eq(Factory::getCompanyID,company.getCompanyID()).set(Factory::getStatus,status);
            factoryMapper.update(null,uw);
            UpdateWrapper<Company> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().eq(Company::getCompanyID,company.getCompanyID()).set(Company::getStatus,status);
            return companyMapper.update(null,updateWrapper);
        }
        return 0;
    }

    @Override
    public Company getCompanyByEmail(@NotNull String email) {
        Company company = null;
        LambdaQueryWrapper<Company> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Company::getEmail,email);
        List<Company> companyList = companyMapper.selectList(lambdaQueryWrapper);
        if(companyList.size()==1){
            company = companyList.get(0);
        }
        return company;
    }

    @Override
    public Company getCompany(Long CompanyID) {
        if(CompanyID!=null) {
            return companyMapper.selectById(CompanyID);
        }
        return null;
    }

    @Override
    public boolean emailExist(@NotNull String email) {
        Company company = null;
        LambdaQueryWrapper<Company> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Company::getEmail,email);
        List<Company> companyList = companyMapper.selectList(lambdaQueryWrapper);
        if(companyList.size()==1){
            company = companyList.get(0);
        }
        return company!=null;
    }

    @Override
    public boolean nameExist(@NotNull String name) {
        LambdaQueryWrapper<Company> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Company::getCompanyName,name);
        List<Company> companyList = companyMapper.selectList(lambdaQueryWrapper);
        return companyList.size()>0;
    }

    @Override
    public List<OrderItem> getOrdersByCompanyID(@NotNull Long companyID) {
        LambdaQueryWrapper<OrderItem> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OrderItem::getCompanyID,companyID);
        return orderItemMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public List<OrderItem> getCertainOrder(@NotNull Long companyID, @NotNull Long orderID) {
        LambdaQueryWrapper<OrderItem> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OrderItem::getCompanyID,companyID);
        lambdaQueryWrapper.eq(OrderItem::getOrderID,orderID);
        return orderItemMapper.selectList(lambdaQueryWrapper);
    }
}
